package offer;

/**
 * @Auther ZhanBo
 * @Date 2020/5/19
 */
public class Solution6 {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5};
        ListNode head = new ListNode(arr);
        int[] ints = reversePrint(head);
        for (int a :ints){
            System.out.println(a);
        }
    }

    /**
     * 输入一个链表的头节点，从尾到头反过来返回每个节点的值（用数组返回）。
     * 输入：head = [1,3,2]
     * 输出：[2,3,1]
     * @param head
     * @return
     */
    public  static  int[] reversePrint(ListNode head) {
//        if(head==null)
//            return new int[0];
        ListNode temp =head;
        int size = 0;
        while(temp!=null){
            temp = temp.next;
            size++;
        }
        int [] result = new int [size];
        size--;
        while(head!=null){
            result[size] =head.val;
            size--;
            head=head.next;
        }
        return result;
    }

    public static int[] reversePrint2(ListNode head) {
        ListNode node = head;
        int count = 0;
        while (node != null) {
            ++count;
            node = node.next;
        }
        int[] nums = new int[count];
        node = head;
        for (int i = count - 1; i >= 0; --i) {
            nums[i] = node.val;
            node = node.next;
        }
        return nums;
    }
}
